package com.qianyu.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.MaterialNeed;
import com.qianyu.project.entity.MaterialNeedDetail;
import com.qianyu.project.mapper.MaterialNeedMapper;
import com.qianyu.project.service.IMaterialNeedDetailService;
import com.qianyu.project.service.IMaterialNeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author lyz
 * @date create in 2021/2/6 15:12
 */
@Service
public class MaterialNeedServiceImpl extends ServiceImpl<MaterialNeedMapper, MaterialNeed> implements IMaterialNeedService {
    @Autowired
    private MaterialNeedMapper needMapper;
    @Autowired
    private IMaterialNeedDetailService detailService;

    @Override
    @FlowBegin("material_need")
    public Integer saveMaterialNeed(@FlowEntry("id") MaterialNeed need
            , List<MaterialNeedDetail> details) {
        needMapper.insert(need);
        details.forEach(d->{
            d.setNeedId(need.getId());
        });
        detailService.saveBatch(details);
        return 1;
    }

    /**
     *  高级查询
     * description:  <br/>
     * author: lyz   <br/>
     * date: 2021/2/22 <br/>
     * params:  <br/>
     * return:
     */
    @Override
    public List<MaterialNeed> getMaterialNeedList(Map<String, String> args) {
        QueryWrapper queryWrapper=new QueryWrapper();
        if(StringUtils.isNotBlank(args.get("needSubject"))){
            queryWrapper.like("need_subject",args.get("needSubject"));
        }
        if(StringUtils.isNotBlank(args.get("applyer"))){
            queryWrapper.like("applyer",args.get("applyer"));
        }
        if(StringUtils.isNotBlank(args.get("sdate"))){
            queryWrapper.ge("date",args.get("sdate"));
        }
        if(StringUtils.isNotBlank(args.get("edate"))){
            queryWrapper.le("date",args.get("edate"));
        }
        if(StringUtils.isNotBlank(args.get("approveStatus"))){
            queryWrapper.eq("approve_status",args.get("approveStatus"));
        }
        return list(queryWrapper);
    }

    @Override
    public MaterialNeed getNeedById(String id) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",id);
        return getOne(queryWrapper);
    }
}
